import Vue from 'vue'
import VueRouter from 'vue-router'
import ManagerView from '../views/Manager.vue' //根节点
// import ElementView from '../views/Element.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: ManagerView,
    redirect: '/home', //重定向到主页
    children: [
      {
        path: 'test',
        name: '测试页',
        meta: {
          name: '测试页'
        },
        component: () => import('../views/manager/Test')
      },
      {
        path: 'test2',
        name: '测试页',
        meta: {
          name: '测试页'
        },
        component: () => import('../views/manager/Test2')
      },
      {
        path: 'home',
        name: '首页',
        meta: {
          name: '系统首页' //导航栏显示
        },
        component: () => import('../views/manager/Home')
      },
      {
        path: 'user',
        name: '用户管理',
        meta: {
          name: '用户管理'
        },
        component: () => import('../views/manager/User')
      },
      {
        path: 'setting',
        name: '高级设置',
        meta: {
          name: '高级设置'
        },
        component: () => import('../views/manager/Setting')
      },
      {
        path: '403',
        name: '无权限',
        meta: {
          name: '无权限页'
        },
        component: () => import('../views/manager/Auth')
      },
      {
        path: 'person',
        name: '个人中心',
        meta: {
          name: '个人中心'
        },
        component: () => import('../views/manager/Person')
      },
      {
        path: 'pwd',
        name: '修改密码',
        meta: {
          name: '修改密码'
        },
        component: () => import('../views/manager/Password')
      },
      {
        path: 'doc',
        name: '文档信息',
        meta: {
          name: '文档信息'
        },
        component: () => import('../views/manager/Doc')
      },

      {
        path: 'notice',
        name: '公告信息',
        meta: {
          name: '公告信息'
        },
        component: () => import('../views/manager/Notice')
      },
      {
        path: 'logs',
        name: '日志信息',
        meta: {
          name: '日志信息'
        },
        component: () => import('../views/manager/Logs')
      },
      {
        path: 'charts',
        name: '数据统计',
        meta: {
          name: '数据统计'
        },
        component: () => import('../views/manager/Charts')
      },
      {
        path: 'message/detail',
        name: '消息详情',
        meta: {
          name: '消息详情'
        },
        component: () => import('../views/manager/MessageDetail')
      },
      {
        path: 'fullMsg',
        name: '全部消息',
        meta: {
          name: '全部消息'
        },
        component: () => import('../views/manager/FullMsg')
      },
      {
        path: 'distribute',
        name: '工单分发',
        meta: {
          name: '工单分发'
        },
        component: () => import('../views/manager/Distribute')
      },
      {
        path: 'file',
        name: '附件管理',
        meta: {
          name: '附件管理'
        },
        component: () => import('../views/manager/File')
      },
      {
        path: 'utils',
        name: '常用工具',
        meta: {
          name: '常用工具'
        },
        component: () => import('../views/manager/Utils')
      },
      {
        path: 'bom',
        name: 'Bom清单',
        meta: {
          name: 'Bom清单'
        },
        component: () => import('../views/manager/Bom')
      },
      {
        path: 'codelib',
        name: '历史码库',
        meta: {
          name: '历史码库'
        },
        component: () => import('../views/manager/CodeLib')
      },
      {
        path: '/website',
        name: '官网消息',
        meta: {
          name: '官网消息'
        },
        component: () => import('../views/manager/Website')
      },
      {
        path: '/project',
        name: '研发立项',
        meta: {
          name: '研发立项'
        },
        component: () => import('../views/manager/Project')
      },
      {
        path: '/ai',
        name: '模型训练',
        meta: {
          name: '模型训练'
        },
        component: () => import('../views/manager/Ai')
      },
      {
        path: '/nas',
        name: '数据中心',
        meta: {
          name: '数据中心'
        },
        component: () => import('../views/manager/Nas')
      },
      {
        path: '/sales',
        name: '销售分析',
        meta: {
          name: '销售分析'
        },
        component: () => import('../views/manager/Sales')
      },
      {
        path: '/calculation',
        name: '公式计算书',
        meta: {
          name: '公式计算书'
        },
        component: () => import('../views/manager/Calculation')
      },
      {
        path: '/coderule',
        name: '编码规则',
        meta: {
          name: '自动编码规则'
        },
        component: () => import('../views/manager/maintain/CodeRule')
      },
      {
        path: '/processNode',
        name: '流程节点',
        meta: {
          name: '流程节点设置'
        },
        component: () => import('../views/manager/process/ProcessNode')
      },
      {
        path: '/proPlan',
        name: '生产计划',
        meta: {
          name: '生产计划'
        },
        component: () => import('../views/manager/process/ProPlan')
      },
      {
        path: '/projectPro',
        name: '项目进度',
        meta: {
          name: '项目进度'
        },
        component: () => import('../views/manager/process/ProjectPro')
      },
      {
        path: '/manager/utils/WsChat',
        name: '邮件窗口',
        meta: {
          name: '邮件窗口'
        },
        component: () => import('../views/manager/utils/WsChat.vue')
      }
    ]
  },
  {
    path: '/ele',
    name: '饿了么',
    component: () => import('../views/Element')
  },
  {
    path: '/login',
    name: '登录',
    meta: {
      name: '登录'
    },
    component: () => import('../views/Login')
  },
  {
    path: '/register',
    name: '注册',
    meta: {
      name: '注册'
    },
    component: () => import('../views/Register')
  },
  {
    path: '*',
    name: '404',
    meta: {
      name: '无法访问'
    },
    component: () => import('../views/404')
  },
  {
    path: '/docDetail',
    name: '文档详情',
    meta: {
      name: '文档详情'
    },
    component: () => import('../views/manager/DocDetail')
  },
  {
    path: '/utilsOpen',
    name: '常用工具',
    meta: {
      name: '常用工具'
    },
    component: () => import('../views/manager/Utils')
  },
  {
    path: '/calculationOpen',
    name: '公式计算书',
    meta: {
      name: '公式计算书'
    },
    component: () => import('../views/manager/Calculation')
  },
  {
    path: '/zhangpuhe',
    name: '张普赫计算书',
    meta: {
      name: '张普赫计算书'
    },
    component: () => import('../views/manager/calculation/Zhangpuhe')
  },
  {
    path: '/autocodeOpen',
    name: '自动编码小程序',
    meta: {
      name: '自动编码小程序'
    },
    component: () => import('../views/manager/utils/Autocode')
  }

  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

//hash有#号，history没有#号。
//history会重新请求资源，服务器把vue的路由地址认为资源地址且没有配置重定向就会404
const router = new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  // routes
  mode: 'hash',
  routes: routes
})

//路由守卫：解决页面角色权限问题
router.beforeEach((to, from, next) => {
  let adminPaths = ['/user']
  let userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')

  if (userInfo.role !== '管理员' && adminPaths.includes(to.path)) {
    //跳转路由的函数:若当前用户非管理员，访问管理员页面时，会跳转到无权限页面。
    next('/403')
  } else {
    next()
  }

})

export default router
